home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
bit
/
src
/
xbm.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
5KB
|
177 lines
/*
* $Id: xbm.c,v 0.91 1994/02/20 00:52:53 zhao Pre-Release $
*
*. This file is part of BIT shareware package. After the two weeks of
* free evaluation period, you are encouraged (required) to register
* your copy for a small registration fee, which is $35 for personal use
* and $50 for commercial, government and institutional use.
*
* Copyright(c) 1993, 1994 by T.C. Zhao.
* All rights reserved.
*
* Permission to use, copy, and distribute this software in its entirety
* for non-commercial purposes is hereby granted, provided that the
* above shareware and copyright notices and this permission notice
* appear in all copies and their documentation.
*
* This software may be modified for your own use, but modified versions
* may not be distributed without prior consent of the author.
*
* This software is provided "as is" without expressed or implied
* warranty of any kind.
*
*.
*
* X Window bitmaps
*/
#ifndef NO_XBM
#if !defined(lint) && defined(F_ID)
char *id_xbm = "$Id: xbm.c,v 0.91 1994/02/20 00:52:53 zhao Pre-Release $";
#endif
#include "bit.h"
extern const char *hexdigits;
int
XBM_desc(IPTR im)
{
char tmpstr[256];
int w = -1, h = -1, no_size = 1, c;
while (no_size && fgets(tmpstr, sizeof(tmpstr), im->fp))
{
if (sscanf(tmpstr, "#define %*s %d", &c) == 1)
{
if (strstr(tmpstr, "_width"))
w = c;
else if (strstr(tmpstr, "_height"))
h = c;
no_size = w < 1 || h < 1;
}
}
im->w = w;
im->h = h;
im->colors = im->cmap->colors = 2;
for (w = 0; w < 3; w++)
{
im->cmap->ct[w][0] = PCMAXV;
im->cmap->ct[w][1] = 0;
}
/* skip until we get brace */
while ((h = getc(im->fp)) != EOF && h != '{') /* } VI */
;
return h == EOF ? EOF : 0;
}
int
XBM_load(IPTR im)
{
register int c, ct, i, j, err;
register ci_t *bits;
long rlines;
rlines = progress_report("Loading XBM ...", im->h);
for (j = err = c = 0; j < im->h && !err; j++)
{
bits = ((ci_t **) im->mraster)[im->h - 1 - j];
REPORT(j, rlines);
for (i = ct = 0; i < im->w && !err; i++, ct = (++ct) & 7)
{
if (!ct)
err = (c = readhexint(im->fp)) < 0;
*bits++ = (c & 1);
c = c >> 1;
}
}
if (err)
{
Bark("XBM_load", "Junk in hex stream");
}
remove_progress_report();
return (j > im->h / 2) ? j : -1;
}
#define write0xhex(fp, c) \
do { \
putc('0', fp); \
putc('x', fp); \
putc(hexdigits[(c) >> 4], fp); \
putc(hexdigits[(c) & 15], fp); \
} while(ZERO)
#define Newline \
do { \
if ((len += 5)> 70) \
{ \
putc('\n', fp); \
putc(' ', fp); \
len = 1; \
} \
} while (ZERO)
#include <ctype.h>
int
XBM_dump(IPTR im)
{
char tmpstr[256], *p;
ci_t *bits;
int nbits, k, len, i, j;
FILE *fp = im->fp;
long rlines;
Strncpy(tmpstr, im->ofile, sizeof(tmpstr) - 25);
if ((p = strchr(tmpstr, '.')))
*p = '\0';
if (isdigit(tmpstr[0]))
tmpstr[0] = 'a';
fprintf(im->fp, "#define %s_width %d\n#define %s_height %d\n",
tmpstr, im->w, tmpstr, im->h);
fprintf(im->fp, "static char %sbits[] = {\n ", tmpstr);
rlines = progress_report("Writing XBM ...", im->h);
for (j = 0, len = 1; j < im->h; j++)
{
REPORT(j, rlines);
bits = ((ci_t **) im->mraster)[im->h - 1 - j];
for (i = nbits = k = 0; i < im->w; i++, bits++)
{
k = k >> 1;
if (*bits)
k |= 0x80;
if (++nbits == 8)
{
k &= 255;
write0xhex(fp, k);
if (!((j == (im->h - 1)) && (i == (im->w - 1))))
putc(',', fp);
Newline;
nbits = k = 0;
}
}
/* check for possible padding */
if (nbits)
{
k = k >> (8 - nbits);
k &= 255;
write0xhex(fp, k);
if (j != im->h - 1)
putc(',', fp);
Newline;
}
}
fputs("};\n", fp);
remove_progress_report();
return fflush(fp);
}
#endif